package com.njzxw.fs.commonutil.util;

import com.njzxw.fs.commonutil.bean.BaseUserBean;
import com.njzxw.fs.commonutil.bean.Common;
import com.njzxw.fs.commonutil.repository.UserRep;
import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 获取用户信息
 */
@Service
@Setter
@Getter
public class UserUtil {

    @Autowired
    protected BaseUserBean baseUserBean;

    @Value("${my.sql.clientUserSql}")
    private String clientUserSql;

    @Value("${my.sql.backgroundUserSql}")
    private String backgroundUserSql;

    @Autowired
    private UserRep userRep;

    @Autowired
    protected RedisUtil redisUtil;

    @Autowired
    private Common common;

    /**
     * 获取前台用户ID 会拦截给与请求
     *
     * @return
     */
    public Map<String, Object> getClientUser() {
        String sql = clientUserSql.replaceAll("\\{oper_id\\}", baseUserBean.getOper_id()).replaceAll("\\{token\\}",
                baseUserBean.getToken());
        Map<String, Object> map = userRep.getClientUser(sql);
        return map;
    }

    /**
     * 获取后台用户ID 根据getClienUserType不同 token取不同值
     *
     * @return
     */
    public Map<String, Object> getBackgroundUser() {
        System.out.println("baseUserBean:" + baseUserBean.toString());
        String sql = backgroundUserSql.replaceAll("\\{oper_id\\}", baseUserBean.getOper_id()).replaceAll("\\{token" +
                "\\}", baseUserBean.getToken());
        System.out.println("baseUserBean:\n" + sql + "\n" + backgroundUserSql);
        Map<String, Object> map = userRep.getBackgroundUser(sql);
        return map;
    }

    /**
     * 判断后台权限是否存在,走数据库判断
     *
     * @param roles 多个角色判断只判断是否存在其中一个,多个角色用逗号隔开
     * @return
     */
    public boolean checkRoleExist(String roles) {

        Map<String, Object> userMap = getBackgroundUser();

        String role_id = StringHelper.get(userMap, "role_id");

        String[] role_ids = role_id.split(",");
        String[] roles_ = roles.split(",");

        for (int i = 0; i < role_ids.length; i++) {
            for (int j = 0; j < roles_.length; j++) {
                if (roles_[j].equals(role_ids[i])) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断后台权限是否存在,走redis判断
     *
     * @param roles 多个角色判断只判断是否存在其中一个,多个角色用逗号隔开
     * @return
     */
    public boolean checkRedisRoleExist(String roles) {

        try {
            String objstr = redisUtil.getValue(baseUserBean.getToken(), String.class);

            if (StringUtils.isBlank(objstr)) {
                return false;
            }

            JSONObject json = JSONObject.fromObject(objstr);

            if (json.containsKey("role_id")) {
                String role_id = json.getString("role_id");

                String[] role_ids = role_id.split(",");
                String[] roles_ = roles.split(",");

                for (int i = 0; i < role_ids.length; i++) {
                    for (int j = 0; j < roles_.length; j++) {
                        if (roles_[j].equals(role_ids[i])) {
                            return true;
                        }
                    }
                }
            }
        } catch (Exception e) {

        }

        return false;
    }

    /**
     * 判断后台组织是否存在,走数据库判断
     *
     * @param orgs 多个组织判断只判断是否存在其中一个,多个组织用逗号隔开
     * @return
     */
    public boolean checkOrgExist(String orgs) {

        Map<String, Object> userMap = getBackgroundUser();

        String org_id = StringHelper.get(userMap, "org_id");

        String[] org_ids = org_id.split(",");
        String[] orgs_ = orgs.split(",");

        for (int i = 0; i < org_ids.length; i++) {
            for (int j = 0; j < orgs_.length; j++) {
                if (orgs_[j].equals(org_ids[i])) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断后台组织是否存在,走redis判断
     *
     * @param orgs 多个组织判断只判断是否存在其中一个,多个组织用逗号隔开
     * @return
     */
    public boolean checkRedisOrgExist(String orgs) {

        String objstr = redisUtil.getValue(baseUserBean.getToken(), String.class);

        if (StringUtils.isBlank(objstr)) {
            return false;
        }

        JSONObject json = JSONObject.fromObject(objstr);
        if (json.containsKey("org_id")) {
            String org_id = JSONObject.fromObject(objstr).getString("org_id");

            String[] org_ids = org_id.split(",");
            String[] orgs_ = orgs.split(",");

            for (int i = 0; i < org_ids.length; i++) {
                for (int j = 0; j < orgs_.length; j++) {
                    if (orgs_[j].equals(org_ids[i])) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    /**
     * 如果存在值就是部门管理员
     *
     * @return
     */
    public String checkRedisManageOrg() {
        String objstr = redisUtil.getValue(baseUserBean.getToken(), String.class);

        if (StringUtils.isBlank(objstr)) {
            return "";
        }

        JSONObject json = JSONObject.fromObject(objstr);

        if (json.containsKey("manageOrg")) {
//            String manageOrg = JSONObject.fromObject(objstr).getString("manageOrg");

            return JSONObject.fromObject(objstr).getString("manageOrg");
        } else {
            return "";
        }

    }

    /**
     * 获取当前用户所在商户ID
     *
     * @return
     */
    public String getMerchantId(){

        String user_id = baseUserBean.getOper_id();

        return userRep.getMerchantId(user_id,common.getSystem("MERCHANT.ORG.TIER"));
    }

}
